package com.nio.page;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by houlian.wang on 2016/11/26.
 */

public class ControlLayout extends LinearLayout implements View.OnClickListener {
    public static final int NO_AIR = -1, AIR_0 = 0, AIR_1 = 1, AIR_2 = 2, AIR_3 = 3, AIR_4 = 4;
    private TextView textAir0, textAir1, textAir2, textAir3, textAir4;
    private View air0Layout, air1Layout, air2Layout, air3Layout, air4Layout;
    private RelativeLayout rlAir0, rlAir1, rlAir2, rlAir3, rlAir4;
    //private LinearLayout llPager;
    private NextevPagerView nextevPagerView;
    //private ViewPager viewPager;
    private Context context;
    private List<TextView> tabList;
    private int lastPage;
    private boolean firstStart;

    private static final String TAG = "ControlLayout";

    public ControlLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        rlAir0 = (RelativeLayout) findViewById(R.id.rlAir0);
        rlAir1 = (RelativeLayout) findViewById(R.id.rlAir1);
        rlAir2 = (RelativeLayout) findViewById(R.id.rlAir2);
        rlAir3 = (RelativeLayout) findViewById(R.id.rlAir3);
        rlAir4 = (RelativeLayout) findViewById(R.id.rlAir4);

        textAir0 = (TextView) findViewById(R.id.textAir0);
        textAir1 = (TextView) findViewById(R.id.textAir1);
        textAir2 = (TextView) findViewById(R.id.textAir2);
        textAir3 = (TextView) findViewById(R.id.textAir3);
        textAir4 = (TextView) findViewById(R.id.textAir4);
        //llPager = (LinearLayout) findViewById(R.id.llPager);
        //viewPager = (ViewPager) findViewById(R.id.viewPager);
        nextevPagerView = (NextevPagerView) findViewById(R.id.nextevPagerView);

        air0Layout = View.inflate(context, R.layout.air_0_layout, null);
        air1Layout = View.inflate(context, R.layout.air_1_layout, null);
        air2Layout = View.inflate(context, R.layout.air_2_layout, null);
        air3Layout = View.inflate(context, R.layout.air_3_layout, null);
        air4Layout = View.inflate(context, R.layout.air_4_layout, null);

        rlAir0.setOnClickListener(this);
        rlAir1.setOnClickListener(this);
        rlAir2.setOnClickListener(this);
        rlAir3.setOnClickListener(this);
        rlAir4.setOnClickListener(this);
        tabList = new ArrayList<>();
        tabList.add(textAir0);
        tabList.add(textAir1);
        tabList.add(textAir2);
        tabList.add(textAir3);
        tabList.add(textAir4);
        for (TextView textTab : tabList) {
            textTab.setTextColor(Color.WHITE);
        }

        addPageView(air0Layout, AIR_0);
        addPageView(air1Layout, AIR_1);
        addPageView(air2Layout, AIR_2);
        addPageView(air3Layout, AIR_3);
        addPageView(air4Layout, AIR_4);
        nextevPagerView.addPage(air0Layout, AIR_0);
        nextevPagerView.addPage(air1Layout, AIR_1);
        nextevPagerView.addPage(air2Layout, AIR_2);
        nextevPagerView.addPage(air3Layout, AIR_3);
        nextevPagerView.addPage(air4Layout, AIR_4);
        //showPageView(1);
        nextevPagerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                firstStart = true;
                lastPage = 1;
                nextevPagerView.pagerSelected(1);
                tabList.get(1).setTextColor(Color.BLUE);
                nextevPagerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        nextevPagerView.setOnPageSelectedListener(new NextevPagerView.OnPageSelectedListener() {
            @Override
            public void onPageSelected(int index) {
                Log.d(TAG, "index:" + index);
                if (lastPage != index) {
                    tabList.get(lastPage).setTextColor(Color.WHITE);
                    tabList.get(index).setTextColor(Color.BLUE);
                    lastPage = index;
                }
            }
        });
        nextevPagerView.setOnPageScrollingListener(new NextevPagerView.OnPageScrollingListener() {
            @Override
            public void onPageScrolling(int currentPage, int newPage, float currentScroll, float newScroll) {
                View newView = nextevPagerView.getPage(newPage);
                View currentView = nextevPagerView.getPage(currentPage);
                //newScroll = 0.8f + 0.2f*newScroll;
                //currentScroll = 0.2f+0.8f*currentScroll;
                Log.d(TAG, "currentPage:" + currentPage + ",currentScroll:" + currentScroll + ",newPage:" + newPage + ",newScroll:" + newScroll);
                ViewCompat.setScaleX(newView, newScroll);
                ViewCompat.setScaleY(newView, newScroll);
                ViewCompat.setScaleX(currentView, currentScroll);
                ViewCompat.setScaleY(currentView, currentScroll);

                TextView newTab = tabList.get(newPage);
                TextView currentTab = tabList.get(lastPage);
                /*ViewCompat.setScaleX(newTab, newScroll);
                ViewCompat.setScaleY(newTab, newScroll);
                ViewCompat.setScaleX(currentTab, currentScroll);
                ViewCompat.setScaleY(currentTab, currentScroll);*/
                if (firstStart) {
                    firstStart = false;
                    return;
                }
                /*ArgbEvaluator evaluator = new ArgbEvaluator();
                currentTab.setTextColor(0xFF0000FF);
                int currentEvaluate = (Integer) evaluator.evaluate(currentScroll, 0xFF0000FF, 0xFFFFFFFF);
                currentTab.setTextColor(currentEvaluate);
                newTab.setTextColor(0xFFFFFFFF);
                int newEvaluate = (Integer) evaluator.evaluate(newScroll, 0xFFFFFFFF, 0xFF0000FF);
                newTab.setTextColor(newEvaluate);*/
            }
        });

        if (air0Layout instanceof Air0Layout) {
            ((Air0Layout) air0Layout).setControlLayout(this);
        }
        if (air1Layout instanceof Air1Layout) {
            ((Air1Layout) air1Layout).setControlLayout(this);
        }
        if (air2Layout instanceof Air2Layout) {
            ((Air2Layout) air2Layout).setControlLayout(this);
        }
        if (air3Layout instanceof Air3Layout) {
            ((Air3Layout) air3Layout).setControlLayout(this);
        }
        if (air4Layout instanceof Air4Layout) {
            ((Air4Layout) air4Layout).setControlLayout(this);
        }
    }

    @Override
    public void onClick(View view) {
        Log.d(TAG, "onClick");
        switch (view.getId()) {
            case R.id.rlAir0:
                //showPageView(AIR_0);
                nextevPagerView.pagerSelected(0);
                break;
            case R.id.rlAir1:
                //showPageView(AIR_1);
                nextevPagerView.pagerSelected(1);
                break;
            case R.id.rlAir2:
                //showPageView(AIR_2);
                nextevPagerView.pagerSelected(2);
                break;
            case R.id.rlAir3:
                //showPageView(AIR_3);
                nextevPagerView.pagerSelected(3);
                break;
            case R.id.rlAir4:
                //showPageView(AIR_4);
                nextevPagerView.pagerSelected(4);
                break;
        }
    }

    private void addPageView(View layout, int index) {
        //layout.setVisibility(GONE);
        //llPager.addView(layout, index, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    /*public void showPageView(int index) {
        Log.d(TAG, "llPager width:" + llPager.getWidth());
        int lastPage = getCurrentPage();
        if (lastPage != NO_AIR) {
            View page = llPager.getChildAt(lastPage);
            page.setVisibility(GONE);
            TextView textAir = tabList.get(lastPage);
            textAir.setTextColor(getResources().getColor(R.color.colorAccent, null));
        }
        llPager.getChildAt(index).setVisibility(VISIBLE);
        tabList.get(index).setTextColor(getResources().getColor(R.color.colorPrimary, null));
    }*/

    /*public int getCurrentPage() {
        //modify control center by wt 20161123 end
        for (int i = 0; i < llPager.getChildCount(); i++) {
            View page = llPager.getChildAt(i);
            if (page.getVisibility() == View.VISIBLE) {
                return i;
            }
        }
        return NO_AIR;
    }*/

    /*public void changePage(float deltaRawX) {
        Log.d(TAG, "page:" + getCurrentPage() + ",deltaRawX:" + deltaRawX);
        switch (getCurrentPage()) {
            case AIR_0:
                if (deltaRawX == 0) {
                    return;
                } else if (deltaRawX > 0) {

                } else {
                    showPageView(AIR_1);
                }
                break;
            case AIR_1:
                if (deltaRawX == 0) {
                    return;
                } else if (deltaRawX > 0) {
                    showPageView(AIR_0);
                } else {
                    showPageView(AIR_2);
                }
                break;
            case AIR_2:
                if (deltaRawX == 0) {
                    return;
                } else if (deltaRawX > 0) {
                    showPageView(AIR_1);
                } else {
                    showPageView(AIR_3);
                }
                break;
            case AIR_3:
                if (deltaRawX == 0) {
                    return;
                } else if (deltaRawX > 0) {
                    showPageView(AIR_2);
                } else {
                    showPageView(AIR_4);
                }
                break;
            case AIR_4:
                if (deltaRawX == 0) {
                    return;
                } else if (deltaRawX > 0) {
                    showPageView(AIR_3);
                } else {

                }
                break;
        }

    }*/
}
